{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Power of Three"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #recursion #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #递归 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isPowerOfThree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #3 的幂"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个整数，写一个函数来判断它是否是 3&nbsp;的幂次方。如果是，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>整数 <code>n</code> 是 3 的幂次方需满足：存在整数 <code>x</code> 使得 <code>n == 3<sup>x</sup></code></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 27\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 0\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 9\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 45\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>-2<sup>31</sup> &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>进阶：</strong>你能不使用循环或者递归来完成本题吗？</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [power-of-three](https://leetcode.cn/problems/power-of-three/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [power-of-three](https://leetcode.cn/problems/power-of-three/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['27', '0', '-1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return n > 0 and 3 ** 20 % n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n == 1:\n",
    "            return True\n",
    "        def dfs(n):\n",
    "            if n / 3 == 1:\n",
    "                return True\n",
    "            if n / 3 == 0:\n",
    "                return False\n",
    "\n",
    "            return dfs(n / 3)\n",
    "\n",
    "        return dfs(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n==1:\n",
    "            return True\n",
    "        if n==0:\n",
    "            return False\n",
    "        return self.isPowerOfThree(n/3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n<=0: return False\n",
    "\n",
    "        return math.pow(3, round(math.log(n, 3))) == n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0: return False\n",
    "        if n == 1.: return True\n",
    "        return self.isPowerOfThree(n/3)\n",
    "        # if n <= 0:\n",
    "        #     return False\n",
    "\n",
    "        # import math\n",
    "        # results = math.log(n, 3)\n",
    "        # results = results//1\n",
    "        # print(results)\n",
    "\n",
    "        # return 3**results == n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n==0 or n == 2: return False\n",
    "        return self.isThree(n)\n",
    "\n",
    "    def isThree(self,n):      \n",
    "        if n == 1:\n",
    "            return True\n",
    "        if n==0 or n == 2:\n",
    "            return False\n",
    "\n",
    "        return self.isThree(n/3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n == 0:\n",
    "            return False\n",
    "        if n == 1:\n",
    "            return True\n",
    "        three = self.isPowerOfThree(n / 3)\n",
    "        return three"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n == 1:\n",
    "            return True\n",
    "        elif 0<=n<1 or n % 3 != 0:\n",
    "            return False\n",
    "        return self.isPowerOfThree(n/3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return n > 0 and 3**19 % n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0:\n",
    "            return False\n",
    "        while n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # def isPowerOfThree(self, n: int) -> bool:\n",
    "    #     # 循环的方法\n",
    "    #     if n <= 0:\n",
    "    #         return False\n",
    "    #     for i in range(n):\n",
    "    #         if 3**i < n:\n",
    "    #             continue\n",
    "    #         elif 3**i > n:\n",
    "    #             break\n",
    "    #         else:\n",
    "    #             return True\n",
    "    #     return False\n",
    "\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        # 因为n有取值范围， 可以用最大的3的幂次方进行求解\n",
    "        if n<= 0:\n",
    "            return False\n",
    "        res = 3**19 % n \n",
    "        return res == 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n<1:\n",
    "            return False\n",
    "        while n!=1:\n",
    "            if n%3!=0:\n",
    "                return False\n",
    "            n=n/3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if (n <= 0):\n",
    "            return False\n",
    "        while (n % 3 == 0):\n",
    "            n/=3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n = n // 3\n",
    "        \n",
    "        return n == 1\n",
    "\n",
    "# 作者：程序员赤小豆\n",
    "# 链接：https://leetcode.cn/problems/power-of-three/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n%3 == 0:\n",
    "            n //= 3\n",
    "        return n==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n/3 > 1:\n",
    "            n = n/3\n",
    "        if n/3 == 1.00 or n == 1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n%2==0:\n",
    "            return False\n",
    "        i=0\n",
    "        while 1:\n",
    "            if 3**i==n:\n",
    "                return True\n",
    "            if 3**i>n:\n",
    "                return False\n",
    "            i+=1\n",
    "        return 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n >= 3:\n",
    "            n /= 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n<=0:\n",
    "            return False\n",
    "        t=1\n",
    "        while t<=n:\n",
    "            if t==n:\n",
    "                return True\n",
    "            t=t*3\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return False if n <= 0 or 10460353203 % n != 0 else True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n = n / 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        def dg(n):\n",
    "            if n == 1:\n",
    "                return True\n",
    "            if n >= -1 and n < 1:\n",
    "                return False\n",
    "            return dg(n/3)\n",
    "        return dg(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0:\n",
    "            return False\n",
    "        elif n == 1 or n == 3:\n",
    "            return True\n",
    "        elif n % 3 == 0:\n",
    "            x =  self.isPowerOfThree( n / 3)\n",
    "        else:\n",
    "            return False                \n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        big=3**30\n",
    "        if n>0 and big%n==0:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        temp=3**19\n",
    "        return n>=1 and temp%n==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        a = 1\n",
    "        while a < n:\n",
    "            a *= 3\n",
    "        if a==n:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while (n and n % 3 == 0 ):\n",
    "            n /= 3 \n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0: return False\n",
    "        while n % 3 == 0: n //= 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return n > 0 and 1162261467 % n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        num=1\n",
    "        temp=[]\n",
    "        temp.append(num)\n",
    "        for i in range(1000):\n",
    "            \n",
    "            num=3*num\n",
    "            temp.append(num)\n",
    "        if n in temp:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n < 1:\n",
    "            return False\n",
    "        else:\n",
    "            while n % 3 == 0:\n",
    "                n /= 3\n",
    "        if n == 1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return n > 0 and 1162261467 % n == 0\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n<=0:\n",
    "            return False\n",
    "        while n%3==0:\n",
    "            n//=3\n",
    "        return n==1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0: return False\n",
    "        while n % 3 == 0: n //= 3\n",
    "        return n == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n == 1:\n",
    "            return True\n",
    "        \n",
    "        if n < 3:\n",
    "            return False\n",
    "\n",
    "        # if not isinstance(n, int):\n",
    "        #     return False\n",
    "\n",
    "        # if n == 0:\n",
    "        #     return False\n",
    "\n",
    "        return self.isPowerOfThree(n / 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        # 超时\n",
    "        # for i in range(0, n//2+1):\n",
    "        #     if n==3**i:\n",
    "        #         return True\n",
    "        # return False\n",
    "        if n==0:\n",
    "            return False\n",
    "        # 法2：不断将n除以3，直到n=1。如果过程中n无法被3整除，说明n不是3的幂\n",
    "        while n!=1:\n",
    "            if n % 3 != 0:\n",
    "                return False\n",
    "            else:\n",
    "                n = n // 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        ## 循环、递归\n",
    "        while  n % 3 == 0 and n >= 3:\n",
    "            n = n / 3\n",
    "\n",
    "        return int(n) == 1        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0: return False \n",
    "        x = log10(n) / log10(3)\n",
    "        return x == int(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n == 1:\n",
    "            return True \n",
    "        if n < 1 or n % 3 != 0:\n",
    "            return False \n",
    "        return self.isPowerOfThree(n//3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "        def isPowerOfThree(self, n: int) -> bool: \n",
    "            for i in range(0,31):\n",
    "                if 3**i==n:\n",
    "                    return (True)\n",
    "            else:\n",
    "                return (False)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0:\n",
    "            return False\n",
    "        if n == 1:\n",
    "            return True\n",
    "        while n > 1:\n",
    "            n /= 3\n",
    "        return n == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n = n // 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return n> 0 and 282429536481 % n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n == 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n < 1:\n",
    "            return False\n",
    "        if n > 1:\n",
    "            while n % 3 == 0:\n",
    "                n //= 3\n",
    "        return n == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0: return False\n",
    "        while n > 0:\n",
    "            # print(n)\n",
    "            if n % 3 != 0 and n != 1:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n<=0:\n",
    "            return False\n",
    "        else:\n",
    "            i = 1\n",
    "            while i < n:\n",
    "                i *= 3\n",
    "        return i == n "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n==1:\n",
    "            return True\n",
    "        a=1\n",
    "        while a<=n:\n",
    "            a*=3\n",
    "            if a==n:\n",
    "                return True\n",
    "        return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0:\n",
    "            return False\n",
    "        if n == 1:\n",
    "            return True\n",
    "        if n % 3 != 0:\n",
    "            return False\n",
    "        return self.isPowerOfThree(n // 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n==3 or n==1:\n",
    "            return True\n",
    "        elif n<1:\n",
    "            return False\n",
    "        A=self.isPowerOfThree(n/3)\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n<=0:\n",
    "            return False\n",
    "        while n%3==0:\n",
    "            n=n//3\n",
    "        return n==1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        \n",
    "        if n==3 or n==1:\n",
    "            return True\n",
    "        if n%3 != 0 or n<=0:\n",
    "            return False\n",
    "        else:\n",
    "            return self.isPowerOfThree(n//3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n/3==n//3 and n>1:\n",
    "            n=n/3\n",
    "            if n==1:\n",
    "                return True\n",
    "        return False if n!=1 else True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return False if n <= 0 else (math.log10(n) / math.log10(3)).is_integer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return n > 0 and 3 ** 19 % n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n<=0:\n",
    "            return False\n",
    "        while n%3==0:\n",
    "            n/=3\n",
    "        if n==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0:\n",
    "            return False\n",
    "        if n == 1:\n",
    "            return True\n",
    "        if n % 3 != 0:\n",
    "            return False\n",
    "        while n % 3 == 0:\n",
    "            n //= 3\n",
    "            if n % 3 != 0 and n != 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        return n > 0 and 1162261467 % n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "     while n and n % 3 == 0:\n",
    "         n //= 3\n",
    "     return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n == 0:\n",
    "            return False\n",
    "\n",
    "        if n == 1:\n",
    "            return True\n",
    "        \n",
    "        if n % 3 == 0:\n",
    "            return self.isPowerOfThree(n // 3)\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "\n",
    "        # 1. 常规模拟 超时\n",
    "        if n==1:\n",
    "            return True\n",
    "\n",
    "        if n%3!=0 or n ==0:\n",
    "            return False\n",
    "\n",
    "        \n",
    "        # for k in range(1,n//3+1):\n",
    "        #     if 3**k == n:\n",
    "        #         return True\n",
    "            \n",
    "        # return False\n",
    "\n",
    "        # 2. 对3倍除,直到为1就T,不是1就F\n",
    "        while n>1:\n",
    "            n = n/3\n",
    "        if n!=1:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        x = 0\n",
    "        j = 0\n",
    "        while x <= n:\n",
    "            x = 3**j\n",
    "            j += 1\n",
    "            if n == x:\n",
    "                return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0 :\n",
    "            return False\n",
    "        while n > 1 :\n",
    "            if n % 3 != 0 :\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "      if n <= 0:  \n",
    "        return False\n",
    "      res = round(math.log(n, 3), 9)\n",
    "        \n",
    "      return res - round(res) == 0 \n",
    "\n",
    "\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n and n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0:\n",
    "            return False\n",
    "        while n:\n",
    "            if not n % 3:\n",
    "               n = n // 3\n",
    "            elif n == 1:\n",
    "                return True\n",
    "            else:\n",
    "                return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n==0:\n",
    "            return False\n",
    "        while n%3==0:\n",
    "            n/=3\n",
    "        if n==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "\n",
    "        # 递归\n",
    "        # if n == 1:\n",
    "        #     return True\n",
    "        # else:\n",
    "        #     return False\n",
    "        # return self.isPowerOfThree(n/3)\n",
    "\n",
    "        # 迭代\n",
    "        while n and n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n == 1\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        for i in range(31):\n",
    "            if pow(3,i) == n:\n",
    "                return True\n",
    "            if i == 30:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        a=[1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969,14348907,43046721,129140163,387420489,1162261467]\n",
    "        return n in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        s = 3486784401\n",
    "        if(n>0 and s % n == 0):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        while n > 0  and n % 3 == 0:\n",
    "            n //= 3\n",
    "        return n == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPowerOfThree(self, n: int) -> bool:\n",
    "        if n <= 0: return False\n",
    "        while n % 3 == 0: n //= 3\n",
    "        return n == 1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
